﻿
@{
    var postTitle = ViewData["postTitle"] as post_title;
    ViewData["title"] = $"{postTitle.title}-帖子详情页";
    var postContent = ViewData["content"] as List<post_content> ?? new List<post_content>();
    var postReply = ViewData["reply"] as List<post_reply> ?? new List<post_reply>();
    var pageModel = ViewData["page"] as PageModel;
    var btnId = Guid.NewGuid().ToString("N");
}

<div>
    <div class="container-fluid">
        <div class="row">
            <div style="text-align:center;font-size:12px;color:#666;line-height:200%;">
                <span>楼主: @postTitle.user_name &emsp;主题: @postTitle.title <br />关键字: @postTitle.keywords &emsp;创建日期: @postTitle.create_time.ToLocalTime().ToString("yyyy-MM-dd HH:mm")</span>
            </div>
        </div>
        @{
            if (postContent.Count > 0)
            {
                <div class="row" style="background-color:white;">
                    @Html.Partial("Pagination", pageModel)
                </div>
            }
        }
    </div>

    @{
        for (int i = 0; i < postContent.Count; i++)
        {
            var item = postContent[i];
            if (item != null)
            {
                <div class="container-fluid">
                    <div class="row" style="background-color:#fbfbfd;border-top:1px dashed #E0D0A3;">
                        <div class="col-md-2" style="font-size:10px;">
                            <div style="height:30px;line-height:30px;">@item.uname</div>
                        </div>
                        <div class="col-md-10" style="background-color:white;">
                            @*内容*@
                            <div style="padding-top:5px;min-height:200px;">
                                @Html.Raw(item.content)
                            </div>
                            @*功能按钮*@
                            <div class="text-right">
                                <a href="javascript:;" onclick="PostInform('@item.id')">举报</a>
                                <span style="color:chocolate;">@(i + 1) 楼</span>
                                <span style="color:chocolate;">@item.create_time.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") </span>
                                <a href="javascript:;" class="hftz" id="hftz@(item.id)" onclick="hftz('@item.id')">回复</a>
                            </div>

                            @*回复内容列表*@
                            <div>
                                <ul style="font-size:12px;color:chocolate;">
                                    @{
                                        var replyList = postReply.Where(x => x.post_cid == item.id).ToList();
                                        foreach (var reply in replyList)
                                        {
                                            <li style="height:30px;line-height:30px;">@reply.uname 回复 @item.uname : @reply.content <span style="display:block;float:right;">@reply.create_time.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")</span></li>
                                        }
                                    }
                                </ul>
                            </div>

                            @*添加回复内容*@
                            <div id="httz_div_@item.id" style="display:none;text-align:center;">
                                <form id="PostReplyForm@(item.id)" class="form-horizontal">
                                    <input type="hidden" name="cid" value="@item.id" />@*帖子当前楼层的id*@
                                    <div class="row form-group">
                                        <div class="col-md-12">
                                            <textarea name="content" id="rep_content_@(item.id)" class="form-control" rows="3"></textarea>
                                        </div>
                                    </div>
                                    <div class="row form-group">
                                        <div class="col-md-12 text-right">
                                            <a class="btn btn-xs btn-info" onclick="PostReply('@item.id')">回复</a>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            }
        }
    }

    <div class="container-fluid">
        @{
            if (postContent.Count > 0)
            {
                <div class="row" style="background-color:#eee;">
                    @Html.Partial("Pagination", pageModel)
                </div>
            }
        }
        <div class="row">
            <div style="height:10px;"></div>
            <div id="post_content_editor" style="background-color:white;"></div>
        </div>

        <div class="row">
            <div class="col-md-12 text-right">
                <div style="height:10px;"></div>
                <cache><a id="@btnId" class="btn btn-xs btn-info btn-post-reply">回贴</a></cache>
                <div style="height:10px;"></div>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script type="text/javascript">
        var E = window.wangEditor
        var editor = new E('#post_content_editor')
        // 或者 var editor = new E( document.getElementById('editor') )
        // editor.customConfig.uploadImgShowBase64 = true
        // 将图片大小限制为 3M
        editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024
        editor.customConfig.uploadImgServer = '/upload/user_image'
        // 限制一次最多上传 5 张图片
        editor.customConfig.uploadImgMaxLength = 5
        editor.customConfig.uploadFileName = 'image'
        editor.create();

    // 点击回复帖子按钮
    function hftz(id) {
        var hftz = $("#hftz" + id).html();
        if ("回复" == hftz) {
            $("#hftz" + id).html('收起回复');
            $("#httz_div_" + id).slideDown(500);
        } else {
            $("#hftz" + id).html('回复');
            $("#httz_div_" + id).slideUp(500);
        }
    }

    // 举报涉嫌违规的帖子, id 是帖子主键
    function PostInform(id) {
        if (id.length > 0) {
            $.ajax({
                type: 'post',
                url: '@Url.Action("PostInform")' + '/' + id,
                error: function (request) {
                    alert('请求错误');
                },
                success: function (data) {
                    console.log(data);
                    alert("举报成功,请等待处理");
                }
            })
        }
    }

    // 回复帖子内容,判断是否登录
    function PostReply(id) {
        if ($("#rep_content_" + id).val() == '') {
            alert("请输入回复内容");
            return;
        }

        $.ajax({
            type: "POST",
            url: "@Url.Action("PostReply")",
            data: $("#PostReplyForm" + id).serialize(),
            error: function (request) {
                alert('请求错误');
            },
            success: function (data) {
                if (data.msg == 'ok') {
                    location.href = "@Context.Request.GetAbsoluteUri()";
                }else {
                    console.log(data);
                    alert("保存失败:" + data.data);
                }
            }
        });
    }

        $(".btn-post-reply").click(function () {
            var id = '@postTitle.id';
            if (editor.txt.text().length == 0) {
                utils.ShowError("请输入文字内容");
                return;
            }
            $.ajax({
                type: "POST",
                url: "@Url.Action("ReplyPost")",
                data: {
                    tid: id,
                    content: editor.txt.html()
                },
                error: function (request) {
                    alert(request.responseJSON.data);
                },
                success: function (data) {
                    if (data.msg == 'ok') {
                        var btnId = $(".btn-post-reply").attr('id');
                        $.cookie("post-detail-href-id", btnId);
                        location.href = "@Context.Request.GetAbsoluteUri()";
                    } else {
                        alert("保存失败:" + data);
                    }
                    console.log(data);
                }
            });
        });

        if ($.cookie('post-detail-href-id') && $.cookie('post-detail-href-id').length > 0) {
            location.href = '#' + $.cookie('post-detail-href-id');
            $.cookie('post-detail-href-id', '');
        }
    </script>
}

